/**
 * @Classname ArrBinaryTreeDemo 顺序存储二叉树：数组转树 树转数组
 * @Description TODO
 * @Date 2021/12/3 14:55
 * @Created K024851
 */
public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
    }
}


class ArrBinaryTree {
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder() {
        this.preOrder();
    }

    public void preOrder(int index) {
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空，不能按照二叉树的前序遍历");
        }
        System.out.println(arr[index]);

        //左递归
        if ((index * 2 + 1) < arr.length) {
            preOrder(2 * index + 1);
        }
        //右递归
        if ((index * 2 + 2) < arr.length) {
            preOrder(2 * index + 2);
        }

    }
}